package eddie;

import java.util.ArrayList;

public class PolynomialRoots {

    static ArrayList coef = new ArrayList();
    static ArrayList firstCoefFactors = new ArrayList();
    static ArrayList lastCoefFactors = new ArrayList();
    static ArrayList roots = new ArrayList();
    static int degree;
    static int place = 0;
    static boolean degreeInit = false;

    public static void parseInt(int i) {
        if (degreeInit == false) {
            degree = i;
            degreeInit = true;
        } else {
            coef.add(place, i);
            place++;
            if (place == degree + 1) {
                findRoots();
            }
        }
    }

    public static String textLabel() {
        int multiplicity = PolynomialRoots.degree - PolynomialRoots.place;
        String output;
        if (multiplicity >= 0) {
            output = "Coefficient of x^" + Integer.toString(PolynomialRoots.degree - PolynomialRoots.place) + ":";
        } else if (roots.isEmpty()) {
            output = "No Real Roots";
        } else {
            output = "Roots: ";
            for (int i = 0; i < roots.size() - 1; i++) {
                output += roots.get(i) + ", ";
            }
            output += roots.get(roots.size() - 1);
        }


        return output;
    }

    public static void findRoots() {
        for (double i = 1; i <= Math.abs((int) coef.get(0)); i++) {
            if ((int) coef.get(0) % i == 0) {
                firstCoefFactors.add(i);

            }
        }

        for (double i = 1; i <= Math.abs((int) coef.get(degree)); i++) {
            if ((int) coef.get(degree) % i == 0) {
                lastCoefFactors.add(i);
            }
        }

        for (int i = 0; i < firstCoefFactors.size(); i++) {
            for (int j = 0; j < lastCoefFactors.size(); j++) {

                double fx = 0;
                double root = (double) lastCoefFactors.get(j) / (double) firstCoefFactors.get(i);
                //System.out.print(root + "-");
                for (int k = degree, l = 0; k >= 0; k--, l++) {
                    fx += (int) coef.get(l) * Math.pow(root, k);
                }
                //System.out.println(fx);
                if (fx == 0 && !roots.contains(root)) {
                    roots.add(root);
                    //System.out.println("/" + root);
                }

                fx = 0;

                //System.out.print(-root + "-");
                for (int k = degree, l = 0; k >= 0; k--, l++) {
                    fx += (int) coef.get(l) * Math.pow(-root, k);
                }
                //System.out.println(fx);
                if (fx == 0 && !roots.contains(-root)) {
                    roots.add(-root);
                    //System.out.println("/" + -root);
                }

            }
        }
    }

    static void reset() {
        coef.clear();
        firstCoefFactors.clear();
        lastCoefFactors.clear();
        roots.clear();
        place = 0;
        degreeInit = false;
    }
}
